package com.xiaoxiong.gobang.evaluate;

import android.util.Log;

import com.xiaoxiong.gobang.bean.Player;

/**
 * @author xiaoxiong
 * @Package com.xiaoxiong.gobang.evaluate
 * @Description:
 * @email 249900679@qq.com
 * @date 2016/4/4 15:33
 */
public class SecondEvaluate implements Evaluator {

    private static int f[] = {1, 16, 17, 15};
    private int fc[];
    private int fcb[][];
    private int[] m_board;

    @Override
    public int Evaluate(int[] board, int position, int player) {
        Log.d("evaluate","评估二");
        m_board = board;
        fc = new int[4];
        fcb = new int[4][2];
        for (int i = 0; i < 4; ++i) {
            fc[i] = 1;
            fcb[i][0] = fcb[i][1] = 0;
        }
        return GetScore(position, player);
    }

    private int GetScore(int dis, int player) {
        RecordPositionInformation(dis, player);
        int score = 0;
        for (int i = 0; i < 4; ++i) {
            //现有棋子加上空格数量大于5个
            if (fc[i] + fcb[i][0] + fcb[i][1] >= 5) {
                //形成活棋
                if (fcb[i][0] > 0 && fcb[i][1] > 0) {

                    switch (fc[i]) {
                        case 4:
                            //如果能形成活四，比其他什么组合都要好，直接返回
                            score += 99999999;
                            return score;
                        case 3:
                            score += 9999999;
                            break;
                        case 2:
                            score += 99999;
                            break;
                    }
                }else if(fcb[i][0] > 0 || fcb[i][1] > 0){
                    //形成冲棋
                    switch (fc[i]){
                        case 4:
                            score += 999999;
                            break;
                        case 3:
                            score += 9999;
                            break;
                        case 2:
                            score += 999;
                            break;
                        case 1:
                            score += 99;
                            break;
                    }
                }
            }
        }

        return score;
    }

    /**
     * 记录该位置的信息
     */
    private void RecordPositionInformation(int dis, int player) {
        for (int i = 0; i < f.length; ++i) {
            int d = dis + f[i];
            while (m_board[d] == player) {
                fc[i]++;
                d += f[i];
            }
            //前端空格数
            while (m_board[d] == Player.NULLPALYER || m_board[d] == player) {
                fcb[i][0]++;
                //大于5个空格即不用再搜索了
                if (fcb[i][0] > 5) {
                    break;
                }
                d += f[i];
            }
            d = dis - f[i];
            while (m_board[d] == player) {
                fc[i]++;
                d -= f[i];
            }
            //后端空格数
            while (m_board[d] == Player.NULLPALYER || m_board[d] == player) {
                fcb[i][1]++;
                //大于5个空格即不用再搜索了
                if (fcb[i][1] > 5) {
                    break;
                }
                d -= f[i];
            }
        }
    }
}
